package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.ProjectStakeholder;
import com.rfid.project.service.ProjectStakeholderService;
import com.rfid.project.model.Project;
import com.rfid.project.service.ProjectService;
import com.rfid.project.model.ProjectStakeholderType;
import com.rfid.project.service.ProjectStakeholderTypeService;
import com.rfid.project.util.StringUtil;

@Controller
public class ProjectStakeholderController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProjectStakeholderController.class);

	@Autowired
	private ProjectStakeholderService projectStakeholderService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private ProjectStakeholderTypeService projectStakeholderTypeService;

	@RequestMapping(value = "/projectStakeholdermgr")
	public String projectStakeholdermgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<Project> projects = projectService.findAll();
		model.addAttribute("projects", projects);

		List<ProjectStakeholderType> projectStakeholderTypes = projectStakeholderTypeService.findAll();
		model.addAttribute("projectStakeholderTypes", projectStakeholderTypes);
		return "projectStakeholdermgr";
	}

	@RequestMapping(value = "/queryProjectStakeholder")
	public String queryProjectStakeholder(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "projectId", defaultValue = "-1") Long projectId,
			@RequestParam(value = "department", defaultValue = "") String department,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "post", defaultValue = "") String post,
			@RequestParam(value = "personalTel", defaultValue = "") String personalTel,
			@RequestParam(value = "officeTel", defaultValue = "") String officeTel,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "projectStakeholderTypeId", defaultValue = "-1") Long projectStakeholderTypeId,
			@RequestParam(value = "personID", defaultValue = "") Boolean personID,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<ProjectStakeholder> items = projectStakeholderService
				.findAll(projectId, department, name, post, personalTel,
						officeTel, remark, projectStakeholderTypeId, personID,
						pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", items.getTotalElements());
		JSONArray models = new JSONArray();
		for (ProjectStakeholder item : items) {
			JSONObject mod = new JSONObject();
			wrapData(item, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findProjectStakeholder")
	public String findProjectStakeholder(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id,
			Model model) {
		ProjectStakeholder projectStakeholder = projectStakeholderService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(projectStakeholder, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(ProjectStakeholder item, JSONObject mod) {
		mod.put("id", item.getId());
		mod.put("name", item.getName());
		mod.put("department", item.getDepartment());
		mod.put("post", item.getPost());
		mod.put("personalTel", item.getPersonalTel());
		mod.put("officeTel", item.getOfficeTel());
		mod.put("remark", StringUtil.getString(item.getRemark()));
		
		mod.put("projectId", item.getProject().getId());
		mod.put("projectName", item.getProject().getName());
		mod.put("projectStakeholderTypeId", item.getProjectStakeholderType().getId());
		mod.put("projectStakeholderTypeName", item.getProjectStakeholderType().getName());
		
		Long personId = item.getPersonID();
		mod.put("personID", personId == null ? -1 : personId);
	}

	@RequestMapping(value = "/editProjectStakeholder", method = RequestMethod.POST)
	public String editProjectStakeholder(HttpServletRequest request,
			HttpServletResponse response, @Valid ProjectStakeholder projectStakeholder,
			Model model, RedirectAttributes redirectAttributes) {
		projectStakeholderService.saveProjectStakeholder(projectStakeholder);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delProjectStakeholder")
	public String delProjectStakeholder(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.projectStakeholderService.deleteProjectStakeholder(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat();
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
